package com.course.configs;

import com.alibaba.fastjson.JSONObject;
import com.course.model.CourseOrder;
import com.course.service.CourseOrderService;
import com.course.service.RedisService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.StringUtils;

import java.util.Date;

/**
 * Rabbit消费者
 */
@Component
public class RabbitConsumer {
    @Autowired
    CourseOrderService courseOrderService;
    @Autowired
    RedisService redisService;
    @RabbitListener(queues="test")
    public void receive(String message){
        if(!StringUtils.isEmpty(message)){
            JSONObject json = JSONObject.parseObject(message);
            if(json != null && !json.isEmpty()){
                CourseOrder co = courseOrderService.findByUserCourse(json.getInteger("userId"),json.getInteger("courseId"));
                if(co == null){
                    co = new CourseOrder(json.getInteger("courseId"),json.getInteger("userId"),new Date());
                    courseOrderService.addOrUpdate(co);
                }
            }
            System.out.println("消费消息:" + message);
        }
    }
}
